<template>
  <div>
    <el-dialog :title="dialogUser.title" :visible.sync="dialogUser.show" width="40%" @open="openFun">
      <el-form ref="form" :model="formData" label-width="80px" :rules="rules">
          <el-form-item label="登录账号" prop="userName">
            <el-input placeholder="登录账号" v-model="formData.userName"></el-input>
          </el-form-item>
          <el-form-item label="登录密码" prop="password" >
            <!-- <el-input type="password" placeholder="登录密码"/> -->
            <el-input placeholder="登录密码" v-model="formData.password"></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <!-- <el-input placeholder="手机号码" type="phone"/> -->
            <el-input placeholder="手机号码" v-model="formData.phone"></el-input>
          </el-form-item>
          <el-form-item label="分配角色" prop="rid">
            <el-select v-model="formData.rid" multiple style="width:76%;" placeholder="请选择">
              <el-option v-for="item in allRole" :key="item.rid" :label="item.roleName" :value="item.rid"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="性别">
            <el-radio-group v-model="formData.gender">
              <el-radio :label="2">未知</el-radio>
              <el-radio :label="1">男</el-radio>
              <el-radio :label="0">女</el-radio>
            </el-radio-group>
          </el-form-item> -->
          <!-- <el-form-item label="Email:" prop="email">
              <el-input v-model="formData.email"></el-input>
          </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" @click="handleSubmit('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRolesList } from "@/api/system/role";
export default {
  props: {
    dialogUser: Object,
    formData: Object
  },
  data() {
    var checkPhone = (rule, value, callback) => {
      const phoneReg = /^1[3|4|5|7|8][0-9]{9}$/
      if (!value) {
        return callback(new Error('电话号码不能为空'))
      }
      setTimeout(() => {
        // Number.isInteger是es6验证数字是否为整数的方法,但是我实际用的时候输入的数字总是识别成字符串
        // 所以我就在前面加了一个+实现隐式转换
        if (!Number.isInteger(+value)) {
          callback(new Error('请输入数字值'))
        } else {
          if (phoneReg.test(value)) {
            callback()
          } else {
            callback(new Error('电话号码格式不正确'))
          }
        }
      }, 100)
    }
    var checkEmail = (rule, value, callback) => {
      const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
      if (!value) {
        return callback(new Error('邮箱不能为空'))
      }
      setTimeout(() => {
        if (mailReg.test(value)) {
          callback()
        } else {
          callback(new Error('请输入正确的邮箱格式'))
        }
      }, 100)
    }
    return {
      allRole: [],
      options: {
        FontAwesome: false,
        ElementUI: true,
        eIcon: false,//自带的图标，来自阿里妈妈
        eIconSymbol: false,//是否开启彩色图标
        addIconList: [],
        removeIconList: []
      },
      rules: {
        userName: [{ required: true, message: '请输入登录账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        rid: [{ required: true, message: '请选择角色', trigger: 'change' }],
        // buyerPhone: [{ validator: checkPhone, trigger: 'blur' }],
        // email: [ { validator: checkEmail, trigger: 'blur' }],
      }
    }
  },
  methods: {
    // 弹窗打开触发的方法
    openFun() {
      getRolesList().then(res => {
        this.allRole = res.data
      })
    },

    // 点击确定按钮触发
    handleSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.dialogUser.option == 'add') {
            this.$emit("handleSubmit")
          } else {
            this.$emit("handleSubmitEdit")
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 点击取消按钮触发
    resetForm(formName) {
      this.dialogUser.show = false
      this.$refs[formName].resetFields();
    }
  },
}
</script>

<style lang="scss" scoped>
</style>